// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const {pagination} = require('pagination')
const db = uniCloud.database();
const collection = db.collection('address_list')
const dbCmd = db.command
module.exports = {
	_before: function () { // 通用预处理器
	},
	/**
	 * method1方法描述
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */
	async getData(event) {
		let params = {
			dbName: 'address_list',
			...event
		}
		let data = await pagination(params)
		return {
			code: 200,
			msg: '获取地址列表成功',
			...data
		}
	},
	async saveData(event) {
		let params = {
			...event
		}
		let result
		if(!event._id) {
			result = await collection.add(params)
		} else {
			delete params._id
			result = await collection.doc(event._id).update({
			  ...params
			})
		}
		return {
			code: 200,
			msg: '保存成功',
			...result
		}
	},
	async deleteData(event) {
		let result;
		if(event._id) {
			result = await collection.where(event).remove()
			
		} else if(event._ids){
			result = await collection.where({
				_id:dbCmd.in(event._ids)
			}).remove()
		} else if(event.type==='all') {
			result = await collection.where({
			  _id: dbCmd.exists(true)
			}).remove()
		}
		return {
			code: 200,
			msg: '删成功',
			...result
		}
		
	},
	async getDetail(event) {
		let result = await collection.where(event).limit(1).get()
		return {
			code: 200,
			msg: '查询成功',
			...result
		}
	}
	/* 
	method1(param1) {
		// 参数校验，如无参数则不需要
		if (!param1) {
			return {
				errCode: 'PARAM_IS_NULL',
				errMsg: '参数不能为空'
			}
		}
		// 业务逻辑
		
		// 返回结果
		return {
			param1 //请根据实际需要返回值
		}
	}
	*/
}
